package com.example.user.controller;

import com.example.core.annotation.HasPerm;
import com.example.model.response.Result;
import com.example.model.user.Menu;
import com.example.model.user.Role;
import com.example.model.user.RoleMenu;
import com.example.user.service.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/{menuId}")
    public Result get(@PathVariable("menuId") Long menuId) {
        return Result.success(menuService.get(menuId));
    }

    @HasPerm("system:menu:list")
    @GetMapping("/list")
    public Result list(Menu menu) {
        return Result.success(menuService.list(menu));
    }

    @HasPerm("system:menu:list")
    @GetMapping("/listAll")
    public Result listAll( Menu menu) {
//        PageHelper.startPage(pageNum, pageSize);
        return Result.success( menuService.listAll(menu));
//        PageInfo<Menu> pageInfo = new PageInfo<>(list);
//        return Result.success(pageInfo);
    }

    @HasPerm("system:menu:list")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, Menu menu) {
        PageHelper.startPage(pageNum, pageSize);
        List<Menu> list = menuService.list(menu);
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    @PostMapping
    public Result insert(@RequestBody Menu menu) {
        return Result.success(menuService.insert(menu));
    }

    @PostMapping("/insertBatch")
    public Result insertBatch(@RequestBody List<RoleMenu> menus) {
        return Result.success(menuService.insertRoleMenu(menus));
    }

    @PutMapping
    public Result update(@RequestBody Menu menu) {
        return Result.success(menuService.update(menu));
    }

    @DeleteMapping("/{menuId}")
    public Result delete(@PathVariable("menuId") Long menuId) {
        return Result.success(menuService.delete(menuId));
    }

    @GetMapping("/listByRole/{roleId}")
    public Result listByRoleId(@PathVariable("roleId") Long roleId) {
        return Result.success(menuService.listMenuByRoleId(roleId));
    }

}
